using System.ComponentModel.Composition;
using AzureContrib.WindowsAzure.Diagnostics;
using AzureContrib.WindowsAzure.Properties;
using MSMCrashDumps = Microsoft.WindowsAzure.Diagnostics.CrashDumps;

namespace AzureContrib.WindowsAzure.ServiceRuntime
{
	/// <summary>
	/// Wrapper around <see cref="MSMCrashDumps"/> exported as <see cref="ICrashDumps"/> using MEF and <see cref="ExportAttribute"/>.
	/// </summary>
	[Export(typeof(ICrashDumps))]
	internal class CrashDumpsWrapper : ICrashDumps
	{
		private readonly IRoleEnvironment roleEnvironment;
		private readonly ITrace trace;

		[ImportingConstructor]
		public CrashDumpsWrapper([Import(RequiredCreationPolicy = CreationPolicy.Shared)] ITraceFactory traceFactory,
														 [Import(RequiredCreationPolicy = CreationPolicy.Shared)] IRoleEnvironment roleEnvironment)
		{
			Assumes.NotNull(traceFactory, "traceFactory");
			Assumes.NotNull(roleEnvironment, "roleEnvironment");

			trace = traceFactory.Create(GetType());
			this.roleEnvironment = roleEnvironment;
		}

		public void EnableCollection(bool enableFullDumps)
		{
			if (!roleEnvironment.IsAvailable)
			{
				trace.Information(Strings.SettingCrashDumpsEnableCollectionNot, enableFullDumps);
				return;
			}

			trace.Information(Strings.SettingCrashDumpsEnableCollection, enableFullDumps);
			MSMCrashDumps.EnableCollection(enableFullDumps);
		}

		public void EnableCollectionToDirectory(string directory, bool enableFullDumps)
		{
			if (!roleEnvironment.IsAvailable)
			{
				trace.Information(Strings.SettingCrashDumpsEnableCollectionToDirectoryNot, directory, enableFullDumps);
				return;
			}

			trace.Information(Strings.SettingCrashDumpsEnableCollectionToDirectory, directory, enableFullDumps);
			MSMCrashDumps.EnableCollectionToDirectory(directory, enableFullDumps);
		}
	}
}